//
//  inputCell.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/13.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "inputCell.h"

@interface inputCell ()<UITextViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) UITextView *inputView;
@property (nonatomic, strong) UITextView *placeHolder;

@end

@implementation inputCell



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}



- (void)setupSubViews
{
    self.editable = YES;
    
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.font = [UIFont systemFontOfSize:15];
    self.titleLab.textColor = kColor333333;
    self.titleLab.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.titleLab];
    
    self.inputView = [[UITextView alloc] init];
    self.inputView.font = [UIFont systemFontOfSize:15];
    self.inputView.textColor = kColor333333;
    self.inputView.textContainerInset = UIEdgeInsetsMake(5, 0, 5, 0);
    self.inputView.delegate = self;
    self.inputView.scrollEnabled = NO;
    self.inputView.backgroundColor = [UIColor clearColor];
//    self.inputView.editable = self.isEditable;
    [self addSubview:self.inputView];
    
    self.placeHolder = [[UITextView alloc] init];
    self.placeHolder.font = self.inputView.font;
    self.placeHolder.textColor = kColor999999;
    self.placeHolder.textContainerInset = self.inputView.textContainerInset;
    self.placeHolder.editable = NO;
    self.placeHolder.selectable = NO;
    self.placeHolder.scrollEnabled = NO;
    self.placeHolder.backgroundColor = [UIColor clearColor];
    [self addSubview:self.placeHolder];
    
    [self.titleLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.top.equalTo(self).offset(15);
    }];
    
//    [self.inputView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLab.mas_right).offset(10);
        make.right.equalTo(self).offset(-16);
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    [self.placeHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.inputView);
    }];
    
    [self bringSubviewToFront:self.inputView];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (!self.isEditable) {
        id<InputCellTableViewDelegate> delegate = (id<InputCellTableViewDelegate>)self.tableView.delegate;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
        if ([delegate respondsToSelector:@selector(tableView:didSelectTextViewAtIndexPath:)]) {
            [delegate tableView:self.tableView didSelectTextViewAtIndexPath:indexPath];
        }
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.placeHolder.alpha = 0;
    }
    else {
        self.placeHolder.alpha = 1;
    }
    id<InputCellTableViewDelegate> delegate = (id<InputCellTableViewDelegate>)self.tableView.delegate;
    if ([self.tableView.visibleCells containsObject:self]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
        if ([delegate respondsToSelector:@selector(tableView:updataText:indexPath:)]) {
            [delegate tableView:self.tableView updataText:textView.text indexPath:indexPath];
        }
    }
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}


- (void)setText:(NSString *)text
{
    _text = text;
    if (text.length > 0) {
        self.placeHolder.alpha = 0;
    }
    else {
        self.placeHolder.alpha = 1;
    }
    self.inputView.text = text;
}

- (void)setPlaceHolderString:(NSString *)placeHolderString
{
    _placeHolderString = placeHolderString;
    self.placeHolder.text = placeHolderString;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
    _keyboardType = keyboardType;
    self.inputView.keyboardType = keyboardType;
}

@end

@implementation UITableView (InputCell)

- (inputCell *)dequeueReusableInputCellWithIdentifier:(NSString *)identifier
{
    inputCell *cell = [self dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[inputCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.tableView = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

@end
